package cn.edu.zjicm.wordsnet_d.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;
import android.util.Log;
import cn.edu.zjicm.wordsnet_d.BuildConfig;

public class LogUtil {
	static String className;
	static String methodName;
	static int lineNumber;

	private LogUtil() {
		/* Protect from instantiations */
	}

	public static boolean isDebuggable() {
		return BuildConfig.DEBUG;
	}

	private static String createLog(String log) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("[");
		buffer.append(methodName);
		buffer.append(":");
		buffer.append(lineNumber);
		buffer.append("]");
		buffer.append(log);

		return buffer.toString();
	}

	private static void getMethodNames(StackTraceElement[] sElements) {
		className = sElements[1].getFileName();
		methodName = sElements[1].getMethodName();
		lineNumber = sElements[1].getLineNumber();
	}

	public static void e(String message) {
		if (!isDebuggable())
			return;

		getMethodNames(new Throwable().getStackTrace());
		Log.e(className, createLog(message));
	}

	public static void e(String tag, String message) {
		if (!isDebuggable())
			return;
		getMethodNames(new Throwable().getStackTrace());
		Log.e(tag, createLog(message));
	}

	public static void i(String message) {
		if (!isDebuggable())
			return;

		getMethodNames(new Throwable().getStackTrace());
		Log.i(className, createLog(message));
	}

	public static void i(String tag, String message) {
		if (!isDebuggable())
			return;
		getMethodNames(new Throwable().getStackTrace());
		Log.i(tag, createLog(message));
	}

	public static void d(String message) {
		if (!isDebuggable())
			return;

		getMethodNames(new Throwable().getStackTrace());
		Log.d(className, createLog(message));
	}

	public static void d(String tag, String message) {
		if (!isDebuggable())
			return;
		getMethodNames(new Throwable().getStackTrace());
		Log.d(tag, createLog(message));
	}

	public static void v(String message) {
		if (!isDebuggable())
			return;

		getMethodNames(new Throwable().getStackTrace());
		Log.v(className, createLog(message));
	}

	public static void v(String tag, String message) {
		if (!isDebuggable())
			return;
		getMethodNames(new Throwable().getStackTrace());
		Log.v(tag, createLog(message));
	}

	public static void w(String message) {
		if (!isDebuggable())
			return;

		getMethodNames(new Throwable().getStackTrace());
		Log.w(className, createLog(message));
	}

	public static void w(String tag, String message) {
		if (!isDebuggable())
			return;
		getMethodNames(new Throwable().getStackTrace());
		Log.w(tag, createLog(message));
	}

	public static void wtf(String message) {
		if (!isDebuggable())
			return;

		getMethodNames(new Throwable().getStackTrace());
		Log.wtf(className, createLog(message));
	}

	public static String fileName = Environment.getExternalStorageDirectory().toString() + "/iwordnet/log.txt";

	public static String readTxtFile() {
		String read;
		String readStr = "";
		FileReader fileread;
		BufferedReader bufread;
		try {
			fileread = new FileReader(fileName);
			bufread = new BufferedReader(fileread);
			try {
				while ((read = bufread.readLine()) != null) {
					readStr = readStr + read + "\r\n";
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return readStr;
	}

	static public void writeTxtFile(String newStr) {
		// 先读取原有文件内容，然后进行写入操作
		try {
			// 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
			FileWriter writer = new FileWriter(fileName, true);
			writer.write(newStr);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void addLog(String str) {
		// writeTxtFile(str + " " + getRecentTime() + "\n");
	}

	public static void addLog1(String str) {
		writeTxtFile("no混淆：" + str + " " + getRecentTime() + "\n");
	}

	static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static String getRecentTime() {
		return df.format(new Date());
	}

}
